/**********************************************************************************
Copyright (C), by AppleCai
Project                      : Study Kernel driver
Description                  : User Application for cdev
History                      :
CPU and Compiler             : AM335x,GCC
GenDate                      : 2020-11-16
OwnerBy                      : Apple Cai
**********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include "stdlib.h"
#include "string.h"

#define DEV_SG90              "/dev/ApplePWM"
#define PMW_START   1
#define PMW_STOP    2

int main(int argc,char **argv) 
{
    int fd =open(DEV_SG90, O_RDWR);
    if (fd< 0) {
        printf("open"DEV_SG90"failed\n");
        goto exit;
    }

	int val = 1000000;
	long pulse_width =0;
	while(1)
	{
		//#if 0
		int angle=0;
		for(angle=0;angle<180;angle++)
		{
			pulse_width=((angle*11)+500)*1000; 
			if (ioctl(fd,PMW_START, pulse_width) < 0) {
					   printf("oictl:setslave address failed\n");
					   goto close;
			}
			//usleep(2000*angle);
			usleep(2000*5); /* 2ms每度 */
		}
		sleep(1);//1s
		#if 0
		    if(val == 500000)
			{
				val = 1000000;
			}
			else
			{
				val = 500000;
			}
			if (ioctl(fd,PMW_START, val) < 0) {
					   printf("oictl:setslave address failed\n");
					   goto close;
			}
			sleep(1); //1ms
		#endif
	}
 
close:
         close(fd);
exit:
         return 0;

}